const express = require('express')
let model = require("../models/userModel.js")
const md5 = require("md5")
const jwt = require('jsonwebtoken')
let loginRouter = express.Router()
loginRouter.post("/loginUser", async (req, res) => {
    let { username, pwd } = req.body
    pwd = md5(pwd)
    try {
        let result = await model.find({ username, pwd })
        // 根据是否在数据库中存在传入项目判定是否成功
        if (result.length != 0) {
            //发送jwt
            const tokentstr = jwt.sign({ username }, "mytoken", { expiresIn: '12h' })
            res.json({
                statu: 1,
                token: tokentstr
            })
        } else {
            res.json({
                statu: 0
            })
        }
    } catch (error) {
        if (error) {
            console.log(error);
            res.json({
                statu: 2
            })
        }
    }

})
module.exports = loginRouter